package src.project.jumble;


import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class LetterTile extends Button implements OnTouchListener {

	public final int WIDTH = 40;
	public final int HEIGHT = 40;
	
	private Jumble parent;
	private String letter;
	
	private float x = 0;
	private float y = 0;
	private AnimationSet animations;
	private Animation ani1;
	private Animation ani2;
	
	
	public interface ButtonHandler{
		void MoveButton(int dir, int pos);
	}
	
	
	public LetterTile(Jumble context, String letter) {
		super(context);
		this.parent = context;
		this.letter = letter;
		
		this.setHeight(HEIGHT);
		this.setWidth(WIDTH);
		
		this.setText(letter);
		animations = new AnimationSet(true);
		
		ani1 = AnimationUtils.loadAnimation(parent, R.anim.ani1);
		ani2 = AnimationUtils.loadAnimation(parent, R.anim.ani2);
		
		animations.addAnimation(ani1);
		animations.addAnimation(ani2);
		
		this.setOnTouchListener(this);
		
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		
		//this.startAnimation(animations);
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			this.setWidth(WIDTH + 10);
			this.setHeight(HEIGHT + 10);
		}
		else if(event.getAction() == MotionEvent.ACTION_UP){
			this.setWidth(WIDTH);
			this.setHeight(HEIGHT);
			parent.ButtonMoved(this);
		}
		
		if(event.getAction() == MotionEvent.ACTION_MOVE)
		{
			x = event.getX();
			y = event.getY();
			
		}	
		return true;
	}

	public float GetX(){return x;}
	public float GetY(){return y;}
	public String GetLetter() { return this.letter; }
	public void SetLetter(String newLetter) { 
		this.letter = newLetter; 
		this.setText(newLetter);
	}
}
